package br.ufrj.neg.reuniao.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.bean.Idioma;
import br.ufrj.neg.constants.General;
import br.ufrj.neg.reuniao.bean.ArtefatoBean;
import br.ufrj.neg.reuniao.bean.ReuniaoBean;
import br.ufrj.neg.reuniao.bean.TipoArtefatoBean;
import br.ufrj.neg.reuniao.bean.TipoReuniaoBean;
import br.ufrj.neg.reuniao.model.ArtefatoModel;
import br.ufrj.neg.reuniao.model.EncaminhamentoModel;
import br.ufrj.neg.reuniao.model.ParticipanteModel;
import br.ufrj.neg.reuniao.model.ReuniaoModel;
import br.ufrj.neg.reuniao.model.TipoArtefatoModel;
import br.ufrj.neg.reuniao.model.TipoReuniaoModel;
import br.ufrj.neg.servlet.PageContext;

public class EditarReuniaoAction extends VelocityViewServlet {

	private static final long serialVersionUID = -1502095409248120929L;

	// private static Logger log = Logger.getLogger("rootLogger");

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ReuniaoModel rm = new ReuniaoModel();
		ParticipanteModel pm = new ParticipanteModel();
		ArtefatoModel am = new ArtefatoModel();
		TipoReuniaoModel trm = new TipoReuniaoModel();
		TipoArtefatoModel tam = new TipoArtefatoModel();
		EncaminhamentoModel em = new EncaminhamentoModel();
		Idioma idioma = (Idioma) request.getAttribute("idioma");
		ReuniaoBean rb = null;

		try {
			HttpSession session = request.getSession(false);
			String novaEdicao = request.getParameter("novaEdicao");
			synchronized (session) {
				session.removeAttribute("NegociacaoBean");
				
				if( novaEdicao==null ){
					rb = (ReuniaoBean) session.getAttribute("ReuniaoBean");
				}
			}
			
			Integer idReuniao;
			if (request.getParameter("idReuniao") != null)
				idReuniao = Integer.parseInt(request.getParameter("idReuniao"));
			else
				idReuniao = (Integer) request.getAttribute("idReuniao");

			setContentType(request, response);
			
			if(rb == null){
				rb = rm.getReuniao(idReuniao);
				
				ArtefatoBean ab = new ArtefatoBean(idioma);
				ab.setReuniao(idReuniao);
								
				ArrayList<ArtefatoBean> abLst = am.listaArtefatoporReuniao(ab);
				
				//Formatando as datas de cada artefato do abLst
				for (ArtefatoBean artefatoBean : abLst) {
					artefatoBean.setDataFormatada(idioma.getData(artefatoBean.getData()));
				}
				rb.setArtefatos(abLst);
				rb.setParticipantes(pm.listaParticipanteporReuniao(idReuniao));
				rb.setEncaminhamento(em.getEncaminhamentoReuniao(idReuniao));
			}
			
			TipoArtefatoBean tab = new TipoArtefatoBean(idioma);
			
			Context context = createContext(request, response);
			fillContext(context, request);
			context.put("pageContext", new PageContext(request));
			TipoReuniaoBean trb = new TipoReuniaoBean(idioma);
			context.put("tipoReuniaoDataSet", trm.listaTipoReuniao(trb));
					
			
			context.put("tipoArtefatoDataSet", tam.listaTipoArtefato(tab));
			context.put("reuniao", rb);			
			
			synchronized (session) {
				session.setAttribute("ReuniaoBean", rb);
			}
			
			Template template = getTemplate(General.TEMPLATE_REUNIAO_PATH + "cadastraReuniao.vm");
			mergeTemplate(template, context, response);

		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

}
